package com.animationsp.acs.bean;

import java.text.Collator;
import java.util.Date;
import java.util.Locale;

import com.animationsp.acs.util.Constant;

/**
 * 根据com.qiniu.api.rsf.ListItem的属性重新封装的bean。
 * 因为原ListItem没有提供setter/getter，而freemarker用getter才能得到属性，所以只能重新封装一遍。
 * @author fscai
 * @see com.qiniu.api.rsf.ListItem
 */
public class ListFile implements Comparable<ListFile>{
	private String key;
	private String hash;
	private long fsize;
	private long putTime;
	private String mimeType;
	private String endUser;
	
	// 原com.qiniu.api.rsf.ListItem中没有的属性
	private Date putDate; // 根据putTime生成的日期
	private String orderType; // 排序类型 Constant.SORT_KEY or SORT_DATE or SORT_SIZE
	private String orderASC; // 
	private Collator chCollator = Collator.getInstance(Locale.CHINA);
	private String readableSize; // fsize的化简，策略：有效数字超过4位单位升一级。例如：12345K -> 12M
	
	public ListFile(){
		chCollator.setStrength(Collator.PRIMARY);
		orderType = Constant.SORT_KEY;
		orderASC = Constant.ORDER_ASC;
	}
	
	@Override
	public String toString() {
		StringBuilder sbuf = new StringBuilder();
		sbuf.append("key:").append(this.key);
		sbuf.append(" hash:").append(this.hash);
		sbuf.append(" fsize:").append(this.fsize);
		sbuf.append(" putTime:").append(this.putTime);
		sbuf.append(" mimeType:").append(this.mimeType);
		sbuf.append(" endUser:").append(this.endUser);
		return sbuf.toString();
	}

	
	@Override
	public int compareTo(ListFile lf) {
		int r = 0;
		if(orderType.equals(Constant.SORT_KEY)){
			r = chCollator.compare(this.getKey(),lf.getKey());
		}else if(orderType.equals(Constant.SORT_DATE)){
			r = ((this.putTime - lf.getPutTime())>=0?1:-1);
		}else if(orderType.equals(Constant.SORT_SIZE)){
			r = ((this.fsize - lf.getFsize())>=0?1:-1);
		}
		return (this.orderASC.equals(Constant.ORDER_ASC)?r:-r);
	}


	public String getKey() {
		return key;
	}


	public void setKey(String key) {
		this.key = key;
	}


	public String getHash() {
		return hash;
	}


	public void setHash(String hash) {
		this.hash = hash;
	}


	public long getFsize() {
		return fsize;
	}


	public void setFsize(long fsize) {
		this.fsize = fsize;
		if(fsize < 10000){
			this.readableSize = fsize + "B";
		}else if(fsize < 10000000){
			this.readableSize = fsize + "K";
		}else if(fsize < 10000000000l){
			this.readableSize = fsize + "M";
		}else if(fsize < 10000000000000l){
			this.readableSize = fsize + "G";
		}else if(fsize < 10000000000000000l){
			this.readableSize = fsize + "T";
		}
	}


	public long getPutTime() {
		return putTime;
	}


	public void setPutTime(long putTime) {
		this.putTime = putTime;
		this.putDate = new Date(this.putTime / 10000); //putTime的单位是100ns，1纳秒=0.000001毫秒 ，所以换算成毫秒要除以1000
	}


	public String getMimeType() {
		return mimeType;
	}


	public void setMimeType(String mimeType) {
		this.mimeType = mimeType;
	}


	public String getEndUser() {
		return endUser;
	}


	public void setEndUser(String endUser) {
		this.endUser = endUser;
	}


	public Date getPutDate() {
		return this.putDate;
	}


	public void setPutDate(Date putDate) {
		this.putDate = putDate;
	}

	public String getOrderType() {
		return orderType;
	}

	public void setOrderType(String orderType) {
		this.orderType = orderType;
	}

	public String getOrderASC() {
		return orderASC;
	}

	public void setOrderASC(String orderASC) {
		this.orderASC = orderASC;
	}

	public String getReadableSize() {
		return readableSize;
	}

	public void setReadableSize(String readableSize) {
		this.readableSize = readableSize;
	}
	
	
}
